package com.example.service.impl;

import com.example.annotation.Log;
import com.example.entity.mysql.Resource;
import com.example.entity.vo.*;
import com.example.mapper.DeviceAttendanceMapper;
import com.example.mapper.DeviceMapper;
import com.example.mapper.MoctyMapper;
import com.example.mapper.SFCTEMapper;
import com.example.mapper.pc.ResourceMapper;
import com.example.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DeviceServiceImpl implements DeviceService {
    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private SFCTEMapper sfcteMapper ;
    @Autowired
    private ResourceMapper resourceMapper ;
    @Autowired
    private MoctyMapper moctyMapper ;

    @Autowired
    private DeviceAttendanceMapper attendanceMapper;

    /**
     * 获取设备状态比例
     * @return
     */
    public DeviceRateVo getDeviceRate() {
        DeviceRateVo deviceRateVo =deviceMapper.getDeviceRate();
        return deviceRateVo;
    }

    public List<DeviceSatusVo> getDeviceStatus() {
        List<String > list=new ArrayList<>();
        list.add("压铸机");
        List<DeviceSatusVo>  DeviceSatusVos =deviceMapper.getDeviceStatus(list);
        return DeviceSatusVos;
    }

    @Override
    public List<AttendanceVo> attendance(Integer type, String date) {
        return type==0 ? attendanceMapper.depart0(date):attendanceMapper.depart1(date);
    }

    public List<DeviceProgressVo> progress() {
        //获取派单表数据存在机器语器连接件（Moctyudf05）
        List<DeviceProgressVo> DeviceProgressVos = sfcteMapper.getAll();
        //放入DeviceProgressVos的Moctyudf05成为一个列表
        List<String> list = DeviceProgressVos.stream()
                .map(DeviceProgressVo::getMoctyud05)
                .collect(Collectors.toList());
        // 在mysql resourse表中查找机器信息
        List<Resource> resources = resourceMapper.getRuninngMachine(list);
        // 将机器名称放入map
        Map<String, String> deviceNameMap = resources.stream()
                .collect(Collectors.toMap(Resource::getMoctyudf05, Resource::getDevicename));
        //遍历deviceNameMap
//        deviceNameMap.forEach((k, v) -> System.out.println("====================key: " + k + " value: " + v));

        for (DeviceProgressVo t : DeviceProgressVos) {
            t.setDeviceName(deviceNameMap.get(t.getMoctyud05().trim()));
        }
        return DeviceProgressVos;
    }

//    public OutputVo getOutput() {
//        OutputVo outputVo = deviceMapper.getOutput();
//        return outputVo;
//    }

//    /**
//     * 设备生产效率
//     * @return
//     */
//    public List<ProductivityVo> getProductivity() {
//        //获取设备名称
//        List<ProductivityVo> list= resourceMapper.getDeviceName();
//        return list;
//    }
}
